package com.stu.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.common.R;
import com.stu.pojo.AccountFlow;
import com.stu.pojo.Clazz;
import com.stu.pojo.Student;
import com.stu.pojo.vo.ClazzVo;
import com.stu.pojo.vo.StudentDescVo;
import com.stu.service.IClazzService;
import com.stu.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    IClazzService clazzService;
    @Autowired
    IStudentService studentService;

    //通过class查询班级中的学员
    @GetMapping("/student/{id}")
    public R getStudents(@PathVariable Long id){
        clazzService.getById(id);
        IPage<Student> pageStus = new Page<>(1,1000);
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.eq(Student::getClazzId,id);
        studentService.page(pageStus,qw);
        return R.success(pageStus);
    }
    //导出班级中学生
    @GetMapping("/output/{id}")
    public R output(@PathVariable Long id, @RequestParam Set<String> includeColumnFiledNames,
                    HttpServletResponse response){
        System.out.println(includeColumnFiledNames);
        response.setHeader("Content-Disposition","attachment;filename="+ UUID.randomUUID()+"stuoa.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        LambdaQueryWrapper<Student> qw = new LambdaQueryWrapper<>();
        qw.eq(Student::getClazzId,id);
        List<Student> list = studentService.list(qw);
        EasyExcel.write(out, Student.class).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(list);
        return R.success(list);
    }


    @GetMapping
    public R getAllClazzName(){
        List<Clazz> list = clazzService.list();
        List<String> nameList = list.stream().map(Clazz::getName).collect(Collectors.toList());
        return R.success(nameList);
    }


    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name,String teacher){
        Page<Clazz> clazzPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Clazz> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Clazz::getName,name);
        qw.like(!StringUtils.isEmpty(teacher),Clazz::getTeacher,teacher);
        qw.orderByDesc(Clazz::getUpdateTime);
        qw.orderByAsc(Clazz::getName);
        clazzService.page(clazzPage,qw);
        return R.success(clazzPage);
    }

    @PostMapping
    public R save(@RequestBody Clazz clazz){
        clazzService.save(clazz);
        return R.success("新增成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Clazz clazz = clazzService.getById(id);
        if (clazz != null){
            return R.success(clazz);
        }
        return R.error("没有查询到对应班级信息");
    }

    @PutMapping
    public R update(@RequestBody Clazz clazz){
        boolean flag = clazzService.updateById(clazz);
        return flag ? R.success("修改成功"):R.error("修改失败");
    }
}

